package pl.edu.pw.mini;

import com.sun.star.frame.XFrame;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lib.uno.helper.Factory;
import com.sun.star.lang.XSingleComponentFactory;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.lib.uno.helper.WeakBase;
import javax.swing.JFrame;
import pl.edu.pw.mini.gui.*;
import pl.edu.pw.mini.state.StateManager;

public final class OoGdocsInt extends WeakBase
        implements com.sun.star.lang.XServiceInfo,
        com.sun.star.frame.XDispatchProvider,
        com.sun.star.lang.XInitialization,
        com.sun.star.frame.XDispatch {
    private ZohoFrame zf;
    private GoogleFrame gf;
    private SetupFrame sf;
    private HistoryFrame hf;
    private AboutFrame af;
    
    private final XComponentContext m_xContext;
    private com.sun.star.frame.XFrame m_xFrame;
    private static final String m_implementationName = OoGdocsInt.class.getName();
    private static final String[] m_serviceNames = {
        "com.sun.star.frame.ProtocolHandler" };
    
    
    public OoGdocsInt( XComponentContext context ) {
        m_xContext = context;
        StateManager.setHost(this);
    };
    
    public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
        XSingleComponentFactory xFactory = null;
        
        if ( sImplementationName.equals( m_implementationName ) )
            xFactory = Factory.createComponentFactory(OoGdocsInt.class, m_serviceNames);
        return xFactory;
    }
    
    public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
        return Factory.writeRegistryServiceInfo(m_implementationName,
                m_serviceNames,
                xRegistryKey);
    }
    
    // com.sun.star.lang.XServiceInfo:
    public String getImplementationName() {
        return m_implementationName;
    }
    
    public boolean supportsService( String sService ) {
        int len = m_serviceNames.length;
        
        for( int i=0; i < len; i++) {
            if (sService.equals(m_serviceNames[i]))
                return true;
        }
        return false;
    }
    
    public String[] getSupportedServiceNames() {
        return m_serviceNames;
    }
    
    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,
            String sTargetFrameName,
            int iSearchFlags ) {
        if ( aURL.Protocol.compareTo("pl.edu.pw.mini.oogdocsint:") == 0 ) {
            if ( aURL.Path.compareTo("cmdZoho") == 0 )
                return this;
            if ( aURL.Path.compareTo("cmdGoogle") == 0 )
                return this;
            if ( aURL.Path.compareTo("cmdHistory") == 0 )
                return this;
            if ( aURL.Path.compareTo("cmsSetup") == 0 )
                return this;
            if ( aURL.Path.compareTo("cmdAbout") == 0 )
                return this;        
        }
        return null;
    }
    
    // com.sun.star.frame.XDispatchProvider:
    public com.sun.star.frame.XDispatch[] queryDispatches(
            com.sun.star.frame.DispatchDescriptor[] seqDescriptors ) {
        int nCount = seqDescriptors.length;
        com.sun.star.frame.XDispatch[] seqDispatcher =
                new com.sun.star.frame.XDispatch[seqDescriptors.length];
        
        for( int i=0; i < nCount; ++i ) {
            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,
                    seqDescriptors[i].FrameName,
                    seqDescriptors[i].SearchFlags );
        }
        return seqDispatcher;
    }
    
    // com.sun.star.lang.XInitialization:
    public void initialize( Object[] object )
    throws com.sun.star.uno.Exception {
        if ( object.length > 0 ) {
            m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(
                    com.sun.star.frame.XFrame.class, object[0]);
        }
    }
    
    private void visualiseFrame(JFrame frame){
        frame.setVisible(true);
        frame.toFront();
        //frame.setAlwaysOnTop(true);
    }
    
    // com.sun.star.frame.XDispatch:
    public void dispatch( com.sun.star.util.URL aURL,
            com.sun.star.beans.PropertyValue[] aArguments ) {
        if ( aURL.Protocol.compareTo("pl.edu.pw.mini.oogdocsint:") == 0 ) {
            if ( aURL.Path.compareTo("cmdZoho") == 0 ) {
                if (zf==null){
                    zf = new ZohoFrame();
                }
                visualiseFrame(zf);
                return;
            }
            if ( aURL.Path.compareTo("cmdGoogle") == 0 ) {
                if (gf==null){
                    gf = new GoogleFrame();
                }
                visualiseFrame(gf);
                return;
            }
            if ( aURL.Path.compareTo("cmdHistory") == 0 ) {
                if (hf==null){
                    hf = new HistoryFrame();
                }
                visualiseFrame(hf);
                return;
            }
            if ( aURL.Path.compareTo("cmsSetup") == 0 ) {
                if (sf==null){
                    sf = new SetupFrame();
                }
                visualiseFrame(sf);
                return;
            }
            if ( aURL.Path.compareTo("cmdAbout") == 0 ) {
                if (af==null){
                    af = new AboutFrame();
                }
                visualiseFrame(af);
                return;
            }        
        }
    }
    
    public void addStatusListener( com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL ) {
        // add your own code here
    }
    
    public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,
            com.sun.star.util.URL aURL ) {
        // add your own code here
    }
    
    public XFrame getFrame(){
        return m_xFrame;
    }
    
}
